package com.hzdl.car.mapper;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Menu;
import com.hzdl.car.sqlprovider.MenuSqlProvider;
import com.hzdl.car.vo.MenuVo;
import com.hzdl.car.vo.RoleVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MenuMapper {
    /**
     * 查询所有可使用菜单
     * @param menuVo
     * @return
     */
    @Select("select * from sys_menu where available=#{available}")
    List<Menu> findAllMenu(MenuVo menuVo);

    /**
     * 条件查询可使用菜单
     * @param menuVo
     * @return
     */

    @SelectProvider(type = MenuSqlProvider.class,method = "findMenuBYWhere")
    List<Menu> findMenuBYWhere(MenuVo menuVo);

    /**
     * 插入菜单
     * @param menuVo
     * @return
     */
    @Insert("insert into sys_menu(pId,name,href,open,target,icon,available) " +
            "values(#{pId},#{name},#{href},#{open},#{target},#{icon},#{available})")
    int add(MenuVo menuVo);



    /**
     * 修改
     * @param menuVo
     * @return
     */
    @Update(" update sys_menu set pId = #{pId}, name =#{name} ,href = #{href} " +
            ",open = #{open} , target = #{target} , icon=#{icon}" +
            " ,available = #{available} where id = #{id} ")
    int update(MenuVo menuVo);
    @Select("select count(*) count from sys_menu where pid = #{id}")
    int exitsChildren(Integer id);

    @Delete("delete from sys_menu where id = #{id} ")
    int delById(Integer id);
    @Select("select * from sys_menu where id=#{id}")
    MenuVo findById(Integer id);
    //左右联动
    @SelectProvider(type = MenuSqlProvider.class,method = "findMenuById")
    List<Menu> findMenuById(MenuVo menuVo);
    @Select("select m.* from sys_role r" +
            " inner join sys_role_menu rm on r.roleId=rm.rId" +
            " inner join sys_menu m on rm.mId=m.Id" +
            " where r.roleId=#{roleId}")
    List<Menu> findMenuByRole(RoleVo roleVo);
    @Select("select m.* from sys_menu m" +
            " inner join sys_role_menu rm on m.id=rm.mId" +
            " inner join sys_role r on rm.rId=r.roleId" +
            " inner join sys_role_user ru on r.roleId=ru.rId" +
            " inner join sys_user u on ru.uId=u.userId" +
            " where m.available=#{available}" +
            " and u.userId=#{userId}")
    List<Menu> findAllMenuByUser(@Param("available") Integer available, @Param("userId") Integer userId);
}


